<template>
  <div class="alar-box">
    <div class="add-user-btn" @click="openSnapModel(1)">
      <span>新增预警类型</span>
    </div>
    <el-table
      max-height="734"
      :data="tableData"
      style="
        width: 100%;
        margin-top: 12px;
        background-color: #303136;
        color: #ffff;
      "
      :border="false"
    >
      <el-table-column prop="typeName" label="预警类型名称"> </el-table-column>
      <el-table-column prop="ruleText" label="预警规则">
        <template slot-scope="scope">
          <div v-html="scope.row.msg"></div>

        </template>
      </el-table-column>
      <el-table-column prop="opration" label="操作">
        <template slot-scope="scope">
           <span class="opration-btn" @click="openSnapModel(2,scope.row)">编辑</span>
           <span class="opration-btn" @click="deletesAnAlertType(scope.row)">删除</span>
        </template>
      </el-table-column>
    </el-table>

    <div class="table-pagination">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNo"
        :page-sizes="pageSizeArr"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
      >
      </el-pagination>
    </div>
    <el-dialog
        :close-on-click-modal = false
      :modal-append-to-body="false"
      :title="dialogTitle"
      :visible.sync="visible"
      width="35%"
    >
      <snap-warning-setting
        v-if="visible"
        :modeType="modeType"
        :snapUserInfo="currentUserInfo"
        :typeOfAlertList="typeOfAlertList"
        @onClose="closeModel"
        @onUpdate="onUpdate"
      ></snap-warning-setting
    ></el-dialog>
  </div>
</template>

<script>
import SnapWarningSetting from "./components/SnapWarningSetting.vue";
import {delWarningType, editWarningType, getWarningTypeList,  typeOfAlert, delWarning} from "@/api/warning";
export default {
  components: {
    SnapWarningSetting,
  },
  data() {
    return {
      radio1: 1,

      visible: false,
      dialogTitle: "新增预警",
      modeType: 0,
      currentUserInfo: {},

      tableData: [],
      pageNo: 1,
      pageSize: 10,
      pageSizeArr: [10, 30, 50],
      totalCount: 0,
      typeOfAlertList: [],
    };
  },
  mounted() {
    this.getWarningTypeList();
  },
  methods: {
    async typeOfAlert() {
      let res = await typeOfAlert();
      if (res.code == 200) {
        this.typeOfAlertList = res.data.lists;
      }
    },
    closeModel(e) {
      this.visible = false;
      if (e.isClose) {
        this.tableData = [];
        this.pageNo = 1;
        this.getWarningTypeList();
      }
    },
    async delWarning(data){
      let res = await delWarning({ id: data.id });
      if (res.code == 200) {
        this.$message({
            type: 'success',
            message: '删除成功!'
          });
      }
      this.getWarningTypeList();
    },
    deletesAnAlertType(data) {
      this.$confirm('是否删除该预警类型', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.delWarning(data)
        }).catch(() => {
                    
        });
    },
    openSnapModel(type, data = {}) {
      this.typeOfAlert();
      this.visible = true;
      this.modeType = type;
      if (type == 1) {
        this.dialogTitle = "新增预警类型";
      } else {
        this.dialogTitle = "编辑预警类型";
        this.currentUserInfo = data;
      }
    },
    onUpdate(){
      window.location.reload()
    },
    async getWarningTypeList() {
      let res = await getWarningTypeList({
        pageNo: this.pageNo,
        pageSize: this.pageSize,
      });

      if (res.code == 200) {
        res.data.lists.forEach((item) => {
          item.isWarn = item.isWarning == 1 ? true : false;
        });
        this.tableData = [...res.data.lists];

        this.totalCount = res.data.count;
      }
    },

    //删除人员库
    async delWarningType(data) {
      let res = await delWarningType({ id: data.id });
      if (res.code == 200) {
        this.$message({
          message: "删除成功",
          type: "success",
        });

        this.tableData = [];
        this.pageNo = 1;
        this.getWarningTypeList();
      }
    },

    handleSizeChange(e) {
      this.pageSize = e;
      this.pageNo = 1;
      this.tableData = [];
      this.getSnapUserList();
    },
    handleCurrentChange(e) {
      this.pageNo = e;
      this.tableData = [];
      this.getSnapUserList();
    },
    handleClick(scope) {
      console.log(scope, "tablescope");
    },

    async onChangeInput(data, e) {
      await editWarningType({
        id: data.id,
        typeName: data.typeName,
        isWarning: data.isWarning,
        warningType: e,
      });
    },

    async onChangeSwitch(data, e) {
      await editWarningType({
        id: data.id,
        typeName: data.typeName,
        isWarning: e ? 1 : 0,
        warningType: data.isWarning,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.add-user-btn {
  width: 118px;
  height: 36px;
  text-align: center;
  line-height: 36px;
  background: #5cfbff;
  color: #17181A#17181A;
  border-radius: 4px;
  cursor: pointer;
}
.opration-btn {
  display: inline-block;
  color: #fff;
  background: #40414d;
  border-radius: 4px;
  cursor: pointer;
  padding: 6px 12px;
  margin-right: 16px;
  margin-top: 16px;
}
.level-color {
  display: inline-block;
  width: 16px;
  height: 16px;
}
.table-pagination {
  text-align: right;
  margin: 30px 0;
}
.left20 {
  margin-left: 20px;
}
</style>

<style>
.el-radio__label {
  padding-left: 5px;
}
.el-radio__inner {
  background: transparent;
  border: 1px solid #fff !important;
}
.el-radio {
  color: #fff !important;
}
.el-radio__input.is-checked .el-radio__inner {
  background-color: #5cfbff;
}
.el-radio__input.is-checked + .el-radio__label {
  color: #5cfbff;
}
</style>
